﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessLogicLayer;

namespace PresentationLayer
{
    public partial class frmStores : Form
    {
        #region Properties
        private frmMain MdiForm { get; set; }
        private enumState State { get; set; }
        enum enumState
        {
            View,
            New,
            Edit
        }
        #endregion

        #region Constructors
        public frmStores(frmMain pMdiForm)
        {
            InitializeComponent();
            this.MdiForm = pMdiForm;
        }
        #endregion

        #region Events
        private void frmStores_Load(object sender, EventArgs e)
        {
            this.MdiForm.Event_OnTranslationChanged += new frmMain.OnTranslationChangedHandler(Translate);
            Translate();
            FillListViewStores(txtSearch.Text, chkIncludeClosed.Checked);
            SetButtonsToState(enumState.View);
            SetTextBoxToState(enumState.View);
        }

        private void lsvStores_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetButtonsToState(enumState.View);
            SetTextBoxToState(enumState.View);
            FillFieldsWithStoreDetails();
        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            FillListViewStores(txtSearch.Text, chkIncludeClosed.Checked);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            SetButtonsToState(enumState.View);
            SetTextBoxToState(enumState.View);
            FillFieldsWithStoreDetails();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            SetButtonsToState(enumState.New);
            SetTextBoxToState(enumState.New);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            SetButtonsToState(enumState.Edit);
            SetTextBoxToState(enumState.Edit);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void chkIncludeClosed_CheckedChanged(object sender, EventArgs e)
        {
            FillListViewStores(txtSearch.Text, chkIncludeClosed.Checked);
        }
        #endregion

        #region Public methods
        #endregion

        #region Private methods
        /// <summary>
        /// Translates every visible element.
        /// </summary>
        private void Translate()
        {
            this.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, "Text", this.Text, 0);
            grpSearch.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, grpSearch.Name, grpSearch.Text, 0);
            chkIncludeClosed.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, chkIncludeClosed.Name, chkIncludeClosed.Text, 0);
            grpStoreDetails.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, grpStoreDetails.Name, grpStoreDetails.Text, 0);
            lblStoreName.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, lblStoreName.Name, lblStoreName.Text, 0);
            lblStreet.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, lblStreet.Name, lblStreet.Text, 0);
            lblPostcode.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, lblPostcode.Name, lblPostcode.Text, 0);
            lblCity.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, lblCity.Name, lblCity.Text, 0);
            lblCountry.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, lblCountry.Name, lblCountry.Text, 0);
            lblDateCreated.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, lblDateCreated.Name, lblDateCreated.Text, 0);
            lblDateChanged.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, lblDateChanged.Name, lblDateChanged.Text, 0);
            lblClosed.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, lblClosed.Name, lblClosed.Text, 0);
            grpOptions.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, grpOptions.Name, grpOptions.Text, 0);
            btnCancel.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, btnCancel.Name, btnCancel.Text, 0);
            btnNew.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, btnNew.Name, btnNew.Text, 0);
            btnEdit.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, btnEdit.Name, btnEdit.Text, 0);
            btnSave.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, btnSave.Name, btnSave.Text, 0);
            FillByFields();
            InitializeListViewStores();
        }

        /// <summary>
        /// Only fills the "by"-Fields.
        /// </summary>
        private void FillByFields()
        {
            if (lsvStores.SelectedItems.Count > 0)
            {
                lblCreatedBy.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, lblCreatedBy.Name, lblCreatedBy.Text, 0);
                lblChangedBy.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, lblChangedBy.Name, lblChangedBy.Text, 0);
            }
            else
            {
                lblCreatedBy.Text = "";
                lblChangedBy.Text = "";
            }
        }

        /// <summary>
        /// Initializes the stores ListView.
        /// </summary>
        private void InitializeListViewStores()
        {
            if (lsvStores.Columns.Count > 0)
            {
                lsvStores.Columns.Clear();
            }
            lsvStores.Columns.Add(this.MdiForm.DataAccess.GetTranslation(this.Name, lsvStores.Name + "_Column1_Name", "Name", 0));
            lsvStores.Columns.Add(this.MdiForm.DataAccess.GetTranslation(this.Name, lsvStores.Name + "_Column2_Street", "Street", 0));
            lsvStores.Columns.Add(this.MdiForm.DataAccess.GetTranslation(this.Name, lsvStores.Name + "_Column3_Postcode", "Postcode", 0));
            lsvStores.Columns.Add(this.MdiForm.DataAccess.GetTranslation(this.Name, lsvStores.Name + "_Column4_City", "City", 0));
            lsvStores.Columns.Add(this.MdiForm.DataAccess.GetTranslation(this.Name, lsvStores.Name + "_Column5_Country", "Country", 0));
            lsvStores.View = View.Details;
            lsvStores.FullRowSelect = true;
            lsvStores.HideSelection = false;
            lsvStores.MultiSelect = false;
        }

        /// <summary>
        /// Saves the store.
        /// </summary>
        private void Save()
        {
            if (SaveStore())
            {
                FillListViewStores(txtSearch.Text, chkIncludeClosed.Checked);
                SetButtonsToState(enumState.View);
                SetTextBoxToState(enumState.View);
            }
        }

        /// <summary>
        /// Saves the store depending on the actual state.
        /// </summary>
        /// <returns>State of success.</returns>
        private Boolean SaveStore()
        {
            Boolean Success = false;
            if (CheckIfTextBoxesEmpty() == false)
            {
                switch (this.State)
                {
                    case enumState.New:
                        if (this.MdiForm.DataAccess.StoreExists(txtStoreName.Text, txtStreet.Text, txtPostcode.Text, txtCity.Text, txtCountry.Text) == false)
                        {
                            Success = this.MdiForm.DataAccess.InsertStore(txtStoreName.Text, txtStreet.Text, txtPostcode.Text, txtCity.Text, txtCountry.Text, chkClosed.Checked);
                        }
                        else
                        {
                            MessageBox.Show("The store does allready exist.");
                        }
                        break;
                    case enumState.Edit:
                        Success = this.MdiForm.DataAccess.ChangeStore(GetStoreID(), txtStoreName.Text, txtStreet.Text, txtPostcode.Text, txtCity.Text, txtCountry.Text, chkClosed.Checked);
                        break;
                }
            }
            if (Success == false)
            {
                MessageBox.Show("Error saving store.");
            }
            return Success;
        }

        /// <summary>
        /// Checks the emptyness of the TextBoxes.
        /// </summary>
        /// <returns>True if TextBoxes are empty.</returns>
        private Boolean CheckIfTextBoxesEmpty()
        {
            if (txtStoreName.Text == "")
            {
                return true;
            }

            if (txtStreet.Text == "")
            {
                return true;
            }

            if (txtPostcode.Text == "")
            {
                return true;
            }

            if (txtCity.Text == "")
            {
                return true;
            }

            if (txtCountry.Text == "")
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// Applies the state to the buttons.
        /// </summary>
        /// <param name="pState">The state.</param>
        private void SetButtonsToState(enumState pState)
        {
            this.State = pState;
            switch (this.State)
            {
                case enumState.View:
                    btnCancel.Enabled = false;
                    btnNew.Enabled = true;
                    btnSave.Enabled = false;
                    if (lsvStores.SelectedItems.Count > 0)
                    {
                        if (lsvStores.SelectedItems[0].Index > -1)
                        {
                            btnEdit.Enabled = true;
                        }
                        else
                        {
                            btnEdit.Enabled = false;
                        }
                    }
                    else
                    {
                        btnEdit.Enabled = false;
                    }
                    break;
                case enumState.New:
                    btnCancel.Enabled = true;
                    btnNew.Enabled = false;
                    btnEdit.Enabled = false;
                    btnSave.Enabled = true;
                    break;
                case enumState.Edit:
                    btnCancel.Enabled = true;
                    btnNew.Enabled = false;
                    btnEdit.Enabled = false;
                    btnSave.Enabled = true;
                    break;
            }
        }

        /// <summary>
        /// Applies the state to the TextBoxes.
        /// </summary>
        /// <param name="pState">The state.</param>
        private void SetTextBoxToState(enumState pState)
        {
            this.State = pState;
            switch (this.State)
            {
                case enumState.View:
                    txtSearch.Enabled = true;
                    lsvStores.Enabled = true;
                    txtStoreName.Enabled = false;
                    txtStreet.Enabled = false;
                    txtPostcode.Enabled = false;
                    txtCity.Enabled = false;
                    txtCountry.Enabled = false;
                    chkClosed.Enabled = false;
                    break;
                case enumState.New:
                    txtSearch.Enabled = false;
                    lsvStores.Enabled = false;
                    txtStoreName.Enabled = true;
                    txtStreet.Enabled = true;
                    txtPostcode.Enabled = true;
                    txtCity.Enabled = true;
                    txtCountry.Enabled = true;
                    txtStoreName.Text = "";
                    txtStreet.Text = "";
                    txtPostcode.Text = "";
                    txtCity.Text = "";
                    txtCountry.Text = "";
                    lblDateCreatedVal.Text = "";
                    lblDateChangedVal.Text = "";
                    lblUserCreatedVal.Text = "";
                    lblUserChangedVal.Text = "";
                    lblCreatedBy.Text = "";
                    lblChangedBy.Text = "";
                    chkClosed.Enabled = true;
                    break;
                case enumState.Edit:
                    txtSearch.Enabled = false;
                    lsvStores.Enabled = false;
                    txtStoreName.Enabled = true;
                    txtStreet.Enabled = true;
                    txtPostcode.Enabled = true;
                    txtCity.Enabled = true;
                    txtCountry.Enabled = true;
                    chkClosed.Enabled = true;
                    break;
            }
        }

        /// <summary>
        /// Fills the ListView with stores.
        /// </summary>
        /// <param name="pSearchArgument"></param>
        private void FillListViewStores(String pSearchArgument, Boolean pIncludeClosed)
        {
            //Trim all the spaces.
            pSearchArgument = pSearchArgument.Trim();
            //Delete all ListView items if available.
            if (lsvStores.Items.Count > 0)
            {
                lsvStores.Items.Clear();
            }
            //Get all stores using search criteria from search field.
            ArrayList Stores = this.MdiForm.DataAccess.GetStores(pSearchArgument, pIncludeClosed);
            for (Int32 i = 0; i < Stores.Count; i++)
            {
                //Get store.
                clsStore Store = (clsStore)Stores[i];
                //Create item for ListView.
                ListViewItem Item = new ListViewItem(Store.StoreName);
                Item.SubItems.Add(Store.Street);
                Item.SubItems.Add(Store.Postcode);
                Item.SubItems.Add(Store.City);
                Item.SubItems.Add(Store.Country);
                Item.Tag = Store;
                //Add item to ListView.
                lsvStores.Items.Add(Item);
            }
            //Resize columns adapted to contents.
            lsvStores.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
            //Select the first item if possible.
            if (lsvStores.Items.Count > 0)
            {
                lsvStores.Items[0].Selected = true;
            }
            else
            {
                FillFieldsWithStoreDetails();
            }
        }

        /// <summary>
        /// Gets the actually selected store ID.
        /// </summary>
        /// <returns>The ID of the store.</returns>
        private Int32 GetStoreID()
        {
            if (lsvStores.SelectedItems.Count > 0)
            {
                clsStore Store = (clsStore)lsvStores.SelectedItems[0].Tag;
                return Store.ID;
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        /// Get the store information from the ListView and fill the details into the fields.
        /// </summary>
        private void FillFieldsWithStoreDetails()
        {
            if (lsvStores.SelectedItems.Count > 0)
            {
                clsStore Store = (clsStore)lsvStores.SelectedItems[0].Tag;
                lblDateCreatedVal.Text = Store.DateCreated.ToString();
                lblDateChangedVal.Text = Store.DateChanged.ToString();
                lblUserCreatedVal.Text = Store.UserCreated;
                lblUserChangedVal.Text = Store.UserChanged;
                txtStoreName.Text = Store.StoreName;
                txtStreet.Text = Store.Street;
                txtPostcode.Text = Store.Postcode;
                txtCity.Text = Store.City;
                txtCountry.Text = Store.Country;
                chkClosed.Checked = Store.Closed;
            }
            else
            {
                lblDateCreatedVal.Text = "";
                lblDateChangedVal.Text = "";
                lblUserCreatedVal.Text = "";
                lblUserChangedVal.Text = "";
                txtStoreName.Text = "";
                txtStreet.Text = "";
                txtPostcode.Text = "";
                txtCity.Text = "";
                txtCountry.Text = "";
                chkClosed.Checked = false;
            }
            FillByFields();
        }
        #endregion
    }
}
